//2019240016 刘艺 大数据191 进程管理（2）
#include <windows.h>
#include <process.h>
#include <stdlib.h>
#include <stdio.h>
#include <conio.h>

int number = 1;

unsigned __stdcall ThreadProc1(void * pArguments){
    while (number < 10){

        printf(" sub thread 1 :number = %d\n",number);
        ++number;
    }
    _endthreadex(0);
    return 0;
}

unsigned __stdcall ThreadProc2(void * pArguments){
    while (number < 10  ){
        printf(" sub thread 2 :number = %d\n",number);
        ++number;
    }
    _endthreadex(0);
    return 0;
}


int main(){
    HANDLE hHandles[2];
    unsigned threadID[2];  //此时两个线程同时运行

    hHandles[0] = (HANDLE) _beginthreadex(NULL, 0,&ThreadProc1,
                                          NULL, 0, &threadID[0]);
    hHandles[1] = (HANDLE) _beginthreadex(NULL, 0,&ThreadProc2,
                                          NULL, 0, &threadID[1]);

    WaitForMultipleObjects( 2, hHandles, TRUE, INFINITE );
    printf("main thread : number = %d\n", number);

    //问题设想：当两个进程都开始进行计算，同时得到number=1；两个线程都开始计算，此时无法同步。

    CloseHandle(hHandles[0]);
    CloseHandle(hHandles[1]);

    getchar();
    return 0;

}



